﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebUtils.Models
{
    public class Lvt
    {

        protected string _sqlInsertFmt = @"insert into list_values_tree
(lng, list_id, parent_id, value, status_id) values
(%LNG%, %LIST_ID%, %PARENT_ID%, '%VALUE%', %STATUS_ID%);
";

        protected string _sqlInsertWithIdFmt = @"insert into list_values_tree
(id, lng, list_id, parent_id, value, status_id) values
(%ID%, %LNG%, %LIST_ID%, %PARENT_ID%, '%VALUE%', %STATUS_ID%);
";

        public int Id { get; set; }

        public SymCmsStatus StatusId { get; set; }

        public SymCsmLng Lng { get; set; }

        public int ListId { get; set; }

        public int ParentId { get; set; }

        public string Value { get; set; }

        public bool IsDefault { get; set; }

        public int Priority { get; set; }


        public string LngToIntAsString
        {
            get
            {
                return ((int)Lng).ToString();
            }
        }

        public string StatusIdToIntAsString
        {
            get
            {
                return ((int)StatusId).ToString();
            }
        }

        public Lvt()
        {
            Lng = SymCsmLng.Lt;
            ParentId = 0;
            IsDefault = false;
            Priority = 0;
            StatusId = SymCmsStatus.Approved;

            ListId = -1;
            Id = -1;
        }

        public string ToSql()
        {
            string sqlFmt;
            if(Id > 0)
            {
                sqlFmt = _sqlInsertWithIdFmt.Replace("%ID%", Id.ToString());
            }
            else
            {
                sqlFmt = _sqlInsertFmt;
            }

            return sqlFmt
                .Replace("%LNG%", LngToIntAsString)
                .Replace("%LIST_ID%", ListId.ToString())
                .Replace("%PARENT_ID%", ParentId.ToString())
                .Replace("%VALUE%", Value)
                .Replace("%STATUS_ID%", StatusIdToIntAsString);
        }
    }

}
